package org.bonorsoft.common.util;

import org.bonorsoft.common.constant.StatisticalConstant;

import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.util.*;

/** 功能描述:自定义工具类
 *
 * @vison: 1.0.0
 * @Author: WilliamYe
 * @Date: 2020/11/26 15:51
 */
public class WilliamUtil {

    /**
     * 根据实体转换成list
     * @param obj
     * @return
     */
    public static ArrayList ConvertObjToList(Object obj){
        ArrayList<String> list=new ArrayList<String>();
        if (obj == null)
            return null;
        Field[] fields = obj.getClass().getDeclaredFields();
        try {
            for(int i=0;i<fields.length;i++){
                try {
                    Field f = obj.getClass().getDeclaredField(fields[i].getName());
                    f.setAccessible(true);
                    Object o = f.get(obj);
                    if(oConvertUtils.isEmpty(o)){
                        o = 0;
                    }
                    list.add(o.toString());
                } catch (NoSuchFieldException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 整数相除保留两位小数
     * @param totalStudent
     * @param number
     * @return
     */
    public static String getSchoolPercentage(Integer totalStudent, Integer number) {
        if (number != 0){
            DecimalFormat df = new DecimalFormat("0.00");//格式化小数
            return df.format((float)totalStudent/number);//返回的是String类型
        }else {
            return "0.00";
        }

    }

    /**
     * 判断是否为三区两县
     * @param area
     * @return
     */
    public static boolean isCounties(String area) {
        return area.equals(StatisticalConstant.DONG_YING) || area.equals(StatisticalConstant.GUANG_RAO)
                || area.equals(StatisticalConstant.HE_KOU) || area.equals(StatisticalConstant.LI_JIN)
                || area.equals(StatisticalConstant.KEN_LI) || area.equals(StatisticalConstant.SHI_ZHI);
    }
}
